package com.bw.config;

import static com.bw.config.ExprotCellStyle.*;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.bw.entity.Users;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;



public class ExportUserUtils {

    /**
     * 把数据导出到path
     * @param users
     */
    @SuppressWarnings("deprecation")
    public static byte[] exportUser(List<Users> users, String path) {
        //创建工作簿
        HSSFWorkbook workbook=new HSSFWorkbook();
        //从工作簿里面创建sheet
//		workbook.createSheet();
        HSSFSheet sheet = workbook.createSheet("用户列表");
        //设置sheet
        //sheet.setColumnWidth(2, 100);//设置某一个下标的列宽
        //sheet.setDefaultColumnStyle(column, style);//设置某一列的默认样式
        sheet.setDefaultColumnWidth(20);//设置所有列的列宽
//		sheet.setColumnHidden(columnIndex, hidden);//设置某一列是否隐藏
        //sheet.setDefaultRowHeight((short)(30*20));//设置行高60
        //sheet.setDefaultRowHeightInPoints(30);//设置行高30
        //合并
        CellRangeAddress region1=new CellRangeAddress(0, 0, 0, 2);
        CellRangeAddress region2=new CellRangeAddress(1, 1, 0, 2);
        sheet.addMergedRegion(region1);
        sheet.addMergedRegion(region2);

        //的sheet上创建行
        int rownum=0;
        HSSFRow row01 = sheet.createRow(rownum);
        //在row01上创建单元格
        HSSFCell cell_row01 = row01.createCell(0);
        //向cell_row01写东西
        cell_row01.setCellValue("用户数据");
        //设置标题样式
        HSSFCellStyle titleStyle=createTitleCellStyle(workbook);
        cell_row01.setCellStyle(titleStyle);

        //第二行
        rownum++;
        HSSFRow row02 = sheet.createRow(rownum);
        HSSFCell cell_row02 = row02.createCell(0);
        cell_row02.setCellValue("总数:"+users.size()+"，导出时间:"+new Date().toLocaleString());
        //设置小标题样式
        HSSFCellStyle secondTitleStyle=createSecondTitleStyle(workbook);
        cell_row02.setCellStyle(secondTitleStyle);

        //第三行
        rownum++;
        HSSFRow row03 = sheet.createRow(rownum);
        String[] titles={"用户ID","用户名","用户地址"};
        //得到表头的样式
        HSSFCellStyle tableTitleStyle=createTableTitleStyle(workbook);

        for (int i = 0; i < titles.length; i++) {
            HSSFCell cell = row03.createCell(i);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(tableTitleStyle);
        }

        HSSFCellStyle tableBodyStyle=setRowCellCenter(workbook);
        for (int i = 0; i < users.size(); i++) {
            rownum++;
            HSSFRow row = sheet.createRow(rownum);
            Users user =users.get(i);
            //创建idcell
            HSSFCell idCell = row.createCell(0);
            idCell.setCellValue(user.getUserId());

            idCell.setCellStyle(tableBodyStyle);
            //创建namecell
            HSSFCell nameCell = row.createCell(1);
            nameCell.setCellValue(user.getUserName());
            nameCell.setCellStyle(tableBodyStyle);

            //创建addresscell
            HSSFCell addressCell = row.createCell(2);
            addressCell.setCellValue(user.getUserAge());
            addressCell.setCellStyle(tableBodyStyle);
        }

        //导出数据
        try {
            workbook.write(new File(path));
            return workbook.getBytes();
        } catch (Exception e) {
            e.printStackTrace();
        }finally
        {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }
}
